/**********************************************************************/
/*
   Author: Karan Makkar, Adapted from Michelle's code
   Created: Nov 2023
   Description: Define table outputting programs for outcome regressions tables
   Note: to run, run the corresponding master.do file
*/
/**********************************************************************/

* Helper function: 
* Save sterfile of all estimates in current directory
* Inputs: 
* sterfile: filepath of sterfile to be saved 
  cap program drop save_sterfile
  program define save_sterfile
    version 17.0
    args sterfile 

    cap erase "`sterfile'" // clear
    estimates dir  // display all regs in memory
    
    foreach e in `r(names)' {  // loop over each estimate
      estimates restore `e'  // load estimate
      estimates title: `e'  // name estimate
      estimates save "`sterfile'", append // save estimate
    }

  end 
  
* Flip table
* Inputs:
* x: treatment variable
* var_list: list of variables
* model_list: list of regression models stored in memory
* columns: list of columns in resulting table
* num_columns: list of columns in resulting table

  cap program drop flip_table
  program define flip_table
    version 17.0
    args x var_list model_list columns num_columns

    esttab `model_list', cells(b(fmt(3)) se(fmt(3))) keep(1.`x') s(control_mean N, fmt(3 %9.0fc))

    * Define matrices
    mat C = r(coefs)
    mat D = r(coefs)' //coefficients transposed
    mat S = r(stats)'

    eststo clear
    di "`columns'"
    local i 0

    foreach col of local columns {
    	local ++i
    	local j 0

    	capture matrix drop k
    	capture matrix drop se
    	capture matrix drop dvmean
    	capture matrix drop nobs

    	foreach model of local var_list {

    			matrix tmp = D[2*`num_columns'*`j'+2*`i'-1, 1]
    			matrix list tmp

    			if tmp[1,1] < . {
    				matrix colnames tmp = `model'
    				matrix k = nullmat(k), tmp
    				matrix tmp[1,1] = D[2*`num_columns'*`j'+2*`i', 1]
    				matrix se = nullmat(se), tmp
    				matrix tmp[1,1] = S[`num_columns'*`j'+`i', 1]
    				matrix dvmean = nullmat(dvmean), tmp
    				matrix tmp[1,1] = S[`num_columns'*`j'+`i', 2]
    				matrix nobs = nullmat(nobs), tmp
    			}

    			else {
    				di `i'
    				di `j'
    				di "`model'"
    			}

    		local ++j
      }

      ereturn post k
    	qui estadd matrix se
    	qui estadd matrix dvmean

      * Saving max num obs
		  scalar obs = el(nobs, 1, 1)
      local nrows = rowsof(nobs)
      local ncols = colsof(nobs)
      forvalues k = 1/`nrows'{
        forval l = 1/`ncols' {
          if el(nobs, `k', `l') > obs {
            scalar obs = el(nobs, `k', `l')
          }
        }
      }

    	estadd scalar obs
    	eststo `col'
    }

  end

* Flip table for interaction regs
* Inputs:
* x: treatment variable
* var_list: list of variables
* model_list: list of regression models stored in memory
* columns: list of columns in resulting table
* num_columns: list of columns in resulting table

  cap program drop flip_table_interact
  program define flip_table_interact
    version 17.0
    args x var_list model_list columns num_columns

    esttab `model_list', cells(b(fmt(3)) se(fmt(3))) keep(1.`x') s(control_mean N obs_bygroup pval, fmt(3 %9.0fc %9.0fc 3))

    * Define matrices
    mat C = r(coefs)
    mat D = r(coefs)' //coefficients transposed
    mat S = r(stats)'

    eststo clear
    di "`columns'"
    local i 0

    foreach col of local columns {
    	local ++i
    	local j 0

    	capture matrix drop k
    	capture matrix drop se
    	capture matrix drop dvmean
    	capture matrix drop nobs
      capture matrix drop obs_bygroup
      capture matrix drop pval

    	foreach model of local var_list {

    			matrix tmp = D[2*`num_columns'*`j'+2*`i'-1, 1]
    			matrix list tmp

    			if tmp[1,1] < . {
    				matrix colnames tmp = `model'
    				matrix k = nullmat(k), tmp
    				matrix tmp[1,1] = D[2*`num_columns'*`j'+2*`i', 1]
    				matrix se = nullmat(se), tmp
    				matrix tmp[1,1] = S[`num_columns'*`j'+`i', 1]
    				matrix dvmean = nullmat(dvmean), tmp
    				matrix tmp[1,1] = S[`num_columns'*`j'+`i', 2]
    				matrix nobs = nullmat(nobs), tmp
            matrix tmp[1,1] = S[`num_columns'*`j'+`i', 3]
    				matrix obs_bygroup = nullmat(obs_bygroup), tmp
            matrix tmp[1,1] = S[`num_columns'*`j'+`i', 4]
    				matrix pval = nullmat(pval), tmp
    			}

          else {
    				di `i'
    				di `j'
    				di "`model'"
    			}
          
    		local ++j
      }

      ereturn post k
      qui estadd matrix se
      qui estadd matrix dvmean
      qui estadd matrix pval
      
      * Saving max num obs
      scalar obs = el(obs_bygroup, 1, 1)
      local nrows = rowsof(obs_bygroup)
      local ncols = colsof(obs_bygroup)
      forvalues k = 1/`nrows'{
        forval l = 1/`ncols' {
          if el(obs_bygroup, `k', `l') > obs {
            scalar obs = el(obs_bygroup, `k', `l')
          }
        }
      }
      
      estadd scalar obs    
    	eststo `col'
    }

  end

* Export Table
* Inputs:
* columns: list of columns in resulting table
* table: table name

  cap program drop export_table
  program define export_table
    version 17.0
    args columns table order

    esttab `columns' using ///
      "`table'.tex", replace ///
      cells(b(star fmt(3)) se(par(( )) fmt(3)) dvmean(par([ ]) fmt(3)))  ///
      stats(obs, label("Observations") fmt(%9.0fc)) noobs ///
      star(* 0.10 ** 0.05 *** 0.01) ///
      prehead("") posthead("") ///
      prefoot("\\ ") postfoot("") ///
      nonum not booktabs collabels(none) ///
      mlabels(none) ///
      order(`order') ///
      subs("prakerja_familiar" "Familiar with Prakerja" ///
           "report_applied" "Report Applying to Prakerja" ///
           "report_selectedhh" "HH Receive Winning Prakerja" ///
           "report_selected" "Report Receive Prakerja" ///
           "complete_pk_train" "Completed PK Training" ///
           "pk_improve_skill" "PK Improved Skill" ///
           "train_certif" "Received Training Certificate" ///
           "self_employed" "Self-Employed" ///
           "employed" "\shortstack[l]{\textit{Panel B: Employment} \\ Employed}" ///
           "self_emp_bus_owner" "Self Employed/Business Owner" ///
           "business_owner" "Business Owner" ///
           "perm_worker" "Permanent Worker" ///
           "temp_worker" "Temporary Worker" ///
           "family_unpaid" "Working for Family/Unpaid" ///
           "hours_worked" "Hours Worked" ///
           "jobhours_adj" "Hours Worked"  ///
           "multiple_jobs" "Working Multiple Jobs" ///
           "use_internet_work" "Use Internet at Work" ///
           "use_internet_bank" "Uses Internet for Banking" ///
           "use_internetc" "\shortstack[l]{\textit{Panel C: Internet} \\ Uses Internet}" ///
           "use_internet" "\shortstack[l]{\textit{Panel A: Internet} \\ Uses Internet}" ///
           "job_web" "\shortstack[l]{\textit{Digital Skills \& Comfort} \\ Used Internet on Job}"  ///
           "ind_adj_earnings" "Earnings" ///
           "ind_earnings_pos" "Earnings are Positive" ///
           "hh_adj_earnings" "HH Earnings" ///
           "hh_earnings_pos" "HH Earnings are Positive" ///
           "hourly_adj_wage" "Hourly Wage" ///
           "wage_adj" "Hourly Wage" ///
           "income_adj" "Total Earnings" ///
           "cons_adj_food" "\shortstack[l]{\textit{Panel A: Consumption} \\ Food Consumption per Capita}" ///
           "cons_adj_nfood" "Non-Food Consumption per Capita" ///
           "nfoodcons" "Non-Food Consumption" ///
           "foodcons" "\shortstack[l]{\textit{Panel A: Consumption} \\ Food Consumption}" ///
           "tot_adj_cons" "\shortstack[l]{\textit{Panel A: Consumption} \\ Total Consumption per Capita}" ///
           "totcons" "\shortstack[l]{\textit{Panel A: Consumption} \\ Total Consumption}" ///
           "food_cons_adj" "Food Consumption" ///
           "nfood_cons_adj" "Non-Food Consumption" ///
           "tot_cons_adj" "\shortstack[l]{\textit{Cons. \& Cons. Smoothing} \\ Total Consumption}" ///
           "ladder_now" "Life Satisfaction"  ///
           "job_satisfy_n" "Job Satisfaction"  ///
           "any_buy" "Buy Any Asset" ///
           "any_sell" "Sell Any Asset" ///
           "loan_borrow" "Taken Additional Loans" ///
           "loan_get" "Received gifts" ///
           "loan_give" "Gave gifts" ///
           "consmooth_move" "Moved" /// 
           "depressed" "\shortstack[l]{\textit{Panel E: Psychometric} \\ Feeling Depressed}" ///
           "depress_disint_n" "\shortstack[l]{\textit{Panel E: Psychometric} \\ Feeling Depressed/Disinterested}" ///
           "efficacy_n" "Self Efficacy" /// 
           "accomplish_goals" "Able to Accomplish Goals" /// 
           "covid_wage_loss" "Wage Loss During COVID" ///
           "looking_jobb" "\shortstack[l]{\textit{Panel B: Job Search} \\ Looking for Job}" ///
           "looking_job" "Looking for Job" ///
           "job_search_online" "Used Online Platforms for Job Search" ///
           "job_search" "Searching for Job (Last Week)" ///
           "reg_job_mkt" "Register for Job Market" ///
           "contact_company" "Contact Company for Job" ///
           "advert_online" "Advertise Self for Job" ///
           "search_network" "Contact Network for Job" ///
           "cap_loc_lic" "Collecting Capital" ///
           "any_attempt" "Other Efforts in Job Search" ///
           "prep_new_bus" "Prepping New Business" ///
           "prep_bus_hh" "HH Prepping New Business" ///
           "hh_new_bus" "HH Prepping New Business" ///
           "business_ability" "Business Ability Confidence" ///
           "prog_subs" "Electricity and Water Subsidies During COVID" ///
           "prog_food_assist" "Grocery Assistance During COVID" ///
           "prog_direct_cash" "Direct Cash Assistance During COVID" ///
           "prog_other" "Other Assistance During COVID" ///
           "apply_kp" "Report Apply for Prakerja" ///
           "receive_kp" "Report Receive Prakerja" /// 
           "take_courses" "\shortstack[l]{\textit{Training} \\ Took a course}" ///
            "take_kp_courses" "Took a Prakerja course" ///
            "num_courses" "Number of courses taken" ///
            "course_hours" "Hours of Training" ///
            "course_completed" "Completed at least one course" ///   
            "course_cert_use" "Used certificate in job search" ///  
            "course_cert" "Received min. one course certificate" ///
            "course_pay_kp" "Paid for min. one course with Prakerja" ///  
            "course_online_live" "Took min. one course online live" ///
            "course_online" "Took min. one course online not live" ///     
            "course_offline" "Took min. one course in person" ///
            "ewallet_buy" "Made Any E-wallet Purchase" ///
            "views_gov_help_n" "\shortstack[l]{\textit{Govt Program Beliefs} \\ Govt Responsible for Helping the Poor}" ///
            "views_allocation_n" "Assistance Should Prioritise the Poor" ///
            "win_before_surveyhh" "\shortstack[l]{\textit{Panel A: Program Receipt} \\ HH Received Prakerja (PMO Data)}" ///
            "win_before_survey" "\shortstack[l]{\textit{Panel A: Program Receipt} \\ Received Prakerja (PMO Data)}" ///
            "ever_win_17" "\shortstack[l]{\textit{Program Receipt} \\ Win in Administrative Data}"  ///
            "switch_industry_1" "Changed Industry from Feb '20" ///
            "wtp_online_training" "WTP for \$1 training credit" ///
            "ever_win_47" "\shortstack[l]{\textit{Program Receipt} \\ Win in Administrative Data}"  ///
            "choose_ewallet" "Choose E-Wallet Over Phone-Credit" ///
            "respondent21" "\shortstack[l]{\textit{2021 Online Surevy} \\ Responded to and Completed Survey}" ///
            "respondent22" "\shortstack[l]{\textit{2022 Online Surevy} \\ Responded to and Completed Survey}" ///
            "dur_canister" "Own LPG Canister" ///
            "dur_fridge" "Own Fridge" ///
            "dur_aircon" "Own Air Conditioner" ///
            "dur_waterheat" "Own Water Heater" ///
            "dur_phone" "Own Telephone" ///
            "dur_computer" "Own Computer" ///
            "dur_gold" "Own Gold" ///
            "dur_bike" "Own Motorcycle" ///
            "dur_boat" "Own Boat" ///
            "dur_motorboat" "Own Motorboat" ///
            "dur_car" "Own Car" ///
            "dur_tv" "Own TV" ///
            "dur_land" "Own Land" ///
            "receive_pkh_year" "Received PKH (Family Hope)" ///
            "receive_KKSd" "\shortstack[l]{\textit{Panel D: Program Receipt} \\ Received KKS (Family Card Sejahtera)}" ///
            "receive_KKS" "\shortstack[l]{\textit{Panel B: Program Receipt} \\ Received KKS (Family Card Sejahtera)}" ///
            "nr_assist" "Received Non-Routine Govt Assistance" ///
            "r_assist" "Received Routine Govt Assistance" ///
            "ever_BPNT" "Received BPNT" ///
			"mil_tot_asset" "Total Asset Purchases ($\times 10^6\ \text{IDR}$)" ///
            )    
  end

* Export Table
* Inputs:
* columns: list of columns in resulting table
* table: table name

  cap program drop export_table_online
  program define export_table_online
    version 17.0
    args columns table order

    esttab `columns' using ///
      "`table'.tex", replace ///
      cells(b(star fmt(3)) se(par(( )) fmt(3)) dvmean(par([ ]) fmt(3)))  ///
      stats(obs, label("Observations") fmt(%9.0fc)) noobs ///
      star(* 0.10 ** 0.05 *** 0.01) ///
      prehead("") posthead("") ///
      prefoot("\\ ") postfoot("") ///
      nonum not booktabs collabels(none) ///
      mlabels(none) ///
      order(`order') ///
      subs("prakerja_familiar" "Familiar with Prakerja" ///
           "report_applied" "Report Applying to Prakerja" ///
           "report_selectedhh" "HH Receive Winning Prakerja" ///
           "report_selected" "Report Receive Prakerja" ///
           "complete_pk_train" "Completed PK Training" ///
           "pk_improve_skill" "PK Improved Skill" ///
           "train_certif" "Received Training Certificate" ///
           "self_employed" "Self-Employed" ///
           "employed" "\shortstack[l]{\textit{Panel B: Employment} \\ Employed}" ///
           "self_emp_bus_owner" "Self Employed/Business Owner" ///
           "business_owner" "Business Owner" ///
           "perm_worker" "Permanent Worker" ///
           "temp_worker" "Temporary Worker" ///
           "family_unpaid" "Working for Family/Unpaid" ///
           "hours_worked" "Hours Worked" ///
           "jobhours_adj" "Hours Worked"  ///
           "multiple_jobs" "Working Multiple Jobs" ///
           "use_internet_work" "Use Internet at Work" ///
           "use_internet_bank" "Uses Internet for Banking" ///
           "use_internet" "\shortstack[l]{\textit{Digital Skills \& Comfort} \\ Uses Internet}" ///
           "job_web" "\shortstack[l]{\textit{Panel B: Digital Skills \& Comfort} \\ Used Internet on Job}"  ///
           "ind_adj_earnings" "Earnings" ///
           "ind_earnings_pos" "Earnings are Positive" ///
           "hh_adj_earnings" "HH Earnings" ///
           "hh_earnings_pos" "HH Earnings are Positive" ///
           "hourly_adj_wage" "Hourly Wage" ///
           "wage_adj" "Hourly Wage" ///
           "income_adj" "Total Earnings" ///
           "cons_adj_food" "Food Consumption" ///
           "cons_adj_nfood" "Non-Food Consumption" ///
           "tot_adj_cons" "\shortstack[l]{\textit{Consumption} \\ Total Consumption per Capita}" ///
           "food_cons_adj" "Food Consumption" ///
           "nfood_cons_adj" "Non-Food Consumption" ///
           "tot_cons_adj" "\shortstack[l]{\textit{Panel A: Cons. \& Cons. Smoothing} \\ Total Consumption}" ///
           "ladder_now" "Life Satisfaction"  ///
           "job_satisfy_n" "Job Satisfaction"  ///
           "any_buy" "Buy Any Asset" ///
           "any_sell" "Sell Any Asset" ///
           "loan_borrow" "Taken Additional Loans" ///
           "loan_get" "Received gifts" ///
           "loan_give" "Gave gifts" ///
           "consmooth_move" "Moved" /// 
           "depressed" "\shortstack[l]{\textit{Panel A: Psychometric} \\ Feeling Depressed}" ///
           "depress_disint_n" "\shortstack[l]{\textit{Panel A: Psychometric} \\ Feeling Depressed/Disinterested}" ///
           "efficacy_n" "Self Efficacy" /// 
           "accomplish_goals" "Able to Accomplish Goals" /// 
           "covid_wage_loss" "Wage Loss During COVID" ///
           "looking_job" "Looking for Job" ///
           "job_search_online" "Used Online Platforms for Job Search" ///
           "job_search" "Searching for Job (Last Week)" ///
           "reg_job_mkt" "Register for Job Market" ///
           "contact_company" "Contact Company for Job" ///
           "advert_online" "Advertise Self for Job" ///
           "search_network" "Contact Network for Job" ///
           "cap_loc_lic" "Collecting Capital" ///
           "any_attempt" "Other Efforts in Job Search" ///
           "prep_new_bus" "Prepping New Business" ///
           "prep_bus_hh" "HH Prepping New Business" ///
           "hh_new_bus" "HH Prepping New Business" ///
           "business_ability" "Business Ability Confidence" ///
           "prog_subs" "Electricity and Water Subsidies During COVID" ///
           "prog_food_assist" "Grocery Assistance During COVID" ///
           "prog_direct_cash" "Direct Cash Assistance During COVID" ///
           "prog_other" "Other Assistance During COVID" ///
           "apply_kp" "Report Apply for Prakerja" ///
           "receive_kp" "\shortstack[l]{\textit{Panel B: IV} \\Report Selected in Survey data}" /// 
           "take_courses" "Took a course" ///
            "take_kp_courses" "Took a Prakerja course" ///
            "num_courses" "Number of courses taken" ///
            "course_hours" "Hours of Training" ///
            "course_completed" "Completed at least one course" ///   
            "course_cert_use" "Used certificate in job search" ///  
            "course_cert" "Received min. one course certificate" ///
            "course_pay_kp" "Paid for min. one course with Prakerja" ///  
            "course_online_live" "Took min. one course online live" ///
            "course_online" "Took min. one course online not live" ///     
            "course_offline" "Took min. one course in person" ///
            "ewallet_buy" "Made Any E-wallet Purchase" ///
            "views_gov_help_n" "\shortstack[l]{\textit{Panel C: Govt Program Beliefs} \\ Govt Responsible for Helping the Poor}" ///
            "views_allocation_n" "Assistance Should Prioritise the Poor" ///
            "win_before_surveyhh" "\shortstack[l]{\textit{Program Receipt} \\ HH Received Prakerja (PMO Data)}" ///
            "win_before_survey" "\shortstack[l]{\textit{Program Receipt} \\ Received Prakerja (PMO Data)}" ///
            "ever_win_17" "Win in Administrative Data"  ///
            "switch_industry_1" "Changed Industry from Feb '20" ///
            "wtp_online_training" "WTP for \$1 training credit" ///
            "ever_win_47" "\shortstack[l]{\textit{Panel A: First Stage} \\ Win in Administrative Data}"  ///
            "choose_ewallet" "Choose E-Wallet Over Phone-Credit" ///
            "respondent21" "\shortstack[l]{\textit{2021 Online Surevy} \\ Responded to and Completed Survey}" ///
            "respondent22" "\shortstack[l]{\textit{2022 Online Surevy} \\ Responded to and Completed Survey}" ///
            "bike_buy" "\shortstack[l]{\textit{Panel A: Buy} \\ Buy Motorbike}" ///
            "bike_sell" "\shortstack[l]{\textit{Panel B: Sell} \\ Sell Motorbike}" ///
            "bike_lend" "Lend Motorbike" ///
            "devices_buy" "Buy Electronic Devices" ///
            "devices_sell" "Sell Electronic Devices" ///
            "devices_lend" "Lend Electronic Devices" ///
            "gold_buy" "Buy Gold" ///
            "gold_sell" "Sell Gold" ///
            "gold_lend" "Lend Gold" ///
            "livestock_buy" "Buy Livestock" ///
            "livestock_sell" "Sell Livestock" ///
            "livestock_lend" "Lend Livestock" ///
			"tot_ast_buy_mil" "\shortstack[l]{\textit{Panel B: Asset Value}  \\Total asset purchases (million IDR)}" ///
		    "tot_ast_buy_w_mil" "Total asset purchases, winsorized (million IDR)" ///
	        "tot_ast_sold_mil" "Total asset sales (million IDR)" ///
	        "tot_ast_sold_w_mil" "Total asset sales, winsorized (million IDR)" ///
	        "net_ast_buy_mil" "Net Asset Purchases (million IDR)" ///
	        "net_ast_buy_w_mil" "Net Asset Purchases, Winsorized (million IDR)" ///
			"net_ast_buy_w_thous" "Net Asset Purchases" ///
	        "ast_mon_val_k" "Average monthly asset purchases (thousand IDR)" ///
	        "net_ast_m_val_k" "Average monthly net asset purchases (thousand IDR)" ///
	        "diff_inc_cons" "\shortstack[l]{\textit{Panel A: Income \& Concumption Difference}  \\ Income – Consumption Difference}" ///
			"inc_cons_ast_mon_w" "Income – Consumption – Averg winsorized monthly net asset" ///
	        "inc_cons_ast_mon" "Income – Consumption – Averg monthly net asset" ///
	        "net_ast_m_val_w_k" "Average monthly net asset purchases, winsorized (thousand IDR)" ///
	        "ast_mon_val_w_k" "Average monthly asset purchases, winsorized (thousand IDR)" ///
          "mil_tot_asset" "Total Asset Purchases ($\times 10^6\ \text{IDR}$)" ///
			)    

  end

* Export Table
* Inputs:
* columns: list of columns in resulting table
* table: table name

  cap program drop export_table_lee
  program define export_table_lee
    version 17.0
    args columns table order

    esttab `columns' using ///
      "`table'.tex", replace ///
      cells(b(star fmt(3)) se(par(( )) fmt(3)) dvmean(par([ ]) fmt(3)))  ///
      stats(obs, label("Observations") fmt(%9.0fc)) noobs ///
      star(* 0.10 ** 0.05 *** 0.01) ///
      prehead("") posthead("") ///
      prefoot("\\ ") postfoot("") ///
      nonum not booktabs collabels(none) ///
      mlabels(none) ///
      order(`order') ///
      subs("prakerja_familiar" "Familiar with Prakerja" ///
           "report_applied" "Report Applying to Prakerja" ///
           "report_selectedhh" "HH Receive Winning Prakerja" ///
           "report_selected" "Report Receive Prakerja" ///
           "complete_pk_train" "Completed PK Training" ///
           "pk_improve_skill" "PK Improved Skill" ///
           "train_certif" "Received Training Certificate" ///
           "self_employed" "Self-Employed" ///
           "employed" "\shortstack[l]{\textit{Panel B: Employment} \\ Employed}" ///
           "self_emp_bus_owner" "Self Employed/Business Owner" ///
           "business_owner" "Business Owner" ///
           "perm_worker" "Permanent Worker" ///
           "temp_worker" "Temporary Worker" ///
           "family_unpaid" "Working for Family/Unpaid" ///
           "hours_worked" "Hours Worked" ///
           "jobhours_adj" "Hours Worked"  ///
           "multiple_jobs" "Working Multiple Jobs" ///
           "use_internet_work" "Use Internet at Work" ///
           "use_internet_bank" "Uses Internet for Banking" ///
           "use_internet" "\shortstack[l]{\textit{Digital Skills \& Comfort} \\ Uses Internet}" ///
           "job_web" "\shortstack[l]{\textit{Panel B: Digital Skills \& Comfort} \\ Used Internet on Job}"  ///
           "ind_adj_earnings" "Earnings" ///
           "ind_earnings_pos" "Earnings are Positive" ///
           "hh_adj_earnings" "HH Earnings" ///
           "hh_earnings_pos" "HH Earnings are Positive" ///
           "hourly_adj_wage" "Hourly Wage" ///
           "wage_adj" "Hourly Wage" ///
           "income_adj" "Total Earnings" ///
           "cons_adj_food" "Food Consumption" ///
           "cons_adj_nfood" "Non-Food Consumption" ///
           "tot_adj_cons" "\shortstack[l]{\textit{Consumption} \\ Total Consumption per Capita}" ///
           "food_cons_adj" "Food Consumption" ///
           "nfood_cons_adj" "Non-Food Consumption" ///
           "tot_cons_adj" "\shortstack[l]{\textit{Panel A: Cons. \& Cons. Smoothing} \\ Total Consumption}" ///
           "ladder_now" "Life Satisfaction"  ///
           "job_satisfy_n" "Job Satisfaction"  ///
           "any_buy" "Buy Any Asset" ///
           "any_sell" "Sell Any Asset" ///
           "loan_borrow" "Taken Additional Loans" ///
           "loan_get" "Received gifts" ///
           "loan_give" "Gave gifts" ///
           "consmooth_move" "Moved" /// 
           "depressed" "\shortstack[l]{\textit{Panel A: Psychometric} \\ Feeling Depressed}" ///
           "depress_disint_n" "\shortstack[l]{\textit{Panel A: Psychometric} \\ Feeling Depressed/Disinterested}" ///
           "efficacy_n" "Self Efficacy" /// 
           "accomplish_goals" "Able to Accomplish Goals" /// 
           "covid_wage_loss" "Wage Loss During COVID" ///
           "looking_job" "Looking for Job" ///
           "job_search_online" "Used Online Platforms for Job Search" ///
           "job_search" "Searching for Job (Last Week)" ///
           "reg_job_mkt" "Register for Job Market" ///
           "contact_company" "Contact Company for Job" ///
           "advert_online" "Advertise Self for Job" ///
           "search_network" "Contact Network for Job" ///
           "cap_loc_lic" "Collecting Capital" ///
           "any_attempt" "Other Efforts in Job Search" ///
           "prep_new_bus" "Prepping New Business" ///
           "prep_bus_hh" "HH Prepping New Business" ///
           "hh_new_bus" "HH Prepping New Business" ///
           "business_ability" "Business Ability Confidence" ///
           "prog_subs" "Electricity and Water Subsidies During COVID" ///
           "prog_food_assist" "Grocery Assistance During COVID" ///
           "prog_direct_cash" "Direct Cash Assistance During COVID" ///
           "prog_other" "Other Assistance During COVID" ///
           "apply_kp" "Report Apply for Prakerja" ///
           "receive_kp" "Report Receive Prakerja" /// 
           "take_courses" "Took a course" ///
            "take_kp_courses" "Took a Prakerja course" ///
            "num_courses" "Number of courses taken" ///
            "course_hours" "Hours of Training" ///
            "course_completed" "Completed at least one course" ///   
            "course_cert_use" "Used certificate in job search" ///  
            "course_cert" "Received min. one course certificate" ///
            "course_pay_kp" "Paid for min. one course with Prakerja" ///  
            "course_online_live" "Took min. one course online live" ///
            "course_online" "Took min. one course online not live" ///     
            "course_offline" "Took min. one course in person" ///
            "ewallet_buy" "Made Any E-wallet Purchase" ///
            "views_gov_help_n" "\shortstack[l]{\textit{Panel C: Govt Program Beliefs} \\ Govt Responsible for Helping the Poor}" ///
            "views_allocation_n" "Assistance Should Prioritise the Poor" ///
            "win_before_surveyhh" "\shortstack[l]{\textit{Program Receipt} \\ HH Received Prakerja (PMO Data)}" ///
            "win_before_survey" "\shortstack[l]{\textit{Program Receipt} \\ Received Prakerja (PMO Data)}" ///
            "ever_win_17" "Win in Administrative Data"  ///
            "switch_industry_1" "Changed Industry from Feb '20" ///
            "wtp_online_training" "WTP for \$1 training credit" ///
            "ever_win_47" "Received Prakerja (PMO Data)"  ///
            "choose_ewallet" "Choose E-Wallet Over Phone-Credit" ///
			"net_ast_buy_w_mil" "Net Asset Purchases, Winsorized (million IDR)" ///
			"net_ast_buy_w_thous" "Net Asset Purchases" ///
            "respondent21" "\shortstack[l]{\textit{2021 Online Surevy} \\ Responded to and Completed Survey}" ///
            "respondent22" "\shortstack[l]{\textit{2022 Online Surevy} \\ Responded to and Completed Survey}")    
  end

* Export Table
* Inputs:
* columns: list of columns in resulting table
* table: table name

  cap program drop export_table_lee_noobs
  program define export_table_lee_noobs
    version 17.0
    args columns table order

    esttab `columns' using ///
      "`table'.tex", replace ///
      cells(b(star fmt(3)) se(par(( )) fmt(3)) dvmean(par([ ]) fmt(3)))  ///
      noobs ///
      star(* 0.10 ** 0.05 *** 0.01) ///
      prehead("") posthead("") ///
      prefoot("\\ ") postfoot("") ///
      nonum not booktabs collabels(none) ///
      mlabels(none) ///
      order(`order') ///
      subs("prakerja_familiar" "Familiar with Prakerja" ///
           "report_applied" "Report Applying to Prakerja" ///
           "report_selectedhh" "HH Receive Winning Prakerja" ///
           "report_selected" "Report Receive Prakerja" ///
           "complete_pk_train" "Completed PK Training" ///
           "pk_improve_skill" "PK Improved Skill" ///
           "train_certif" "Received Training Certificate" ///
           "self_employed" "Self-Employed" ///
           "employed" "\shortstack[l]{\textit{Panel B: Employment} \\ Employed}" ///
           "self_emp_bus_owner" "Self Employed/Business Owner" ///
           "business_owner" "Business Owner" ///
           "perm_worker" "Permanent Worker" ///
           "temp_worker" "Temporary Worker" ///
           "family_unpaid" "Working for Family/Unpaid" ///
           "hours_worked" "Hours Worked" ///
           "jobhours_adj" "Hours Worked"  ///
           "multiple_jobs" "Working Multiple Jobs" ///
           "use_internet_work" "Use Internet at Work" ///
           "use_internet_bank" "Uses Internet for Banking" ///
           "use_internet" "\shortstack[l]{\textit{Digital Skills \& Comfort} \\ Uses Internet}" ///
           "job_web" "\shortstack[l]{\textit{Panel B: Digital Skills \& Comfort} \\ Used Internet on Job}"  ///
           "ind_adj_earnings" "Earnings" ///
           "ind_earnings_pos" "Earnings are Positive" ///
           "hh_adj_earnings" "HH Earnings" ///
           "hh_earnings_pos" "HH Earnings are Positive" ///
           "hourly_adj_wage" "Hourly Wage" ///
           "wage_adj" "Hourly Wage" ///
           "income_adj" "Total Earnings" ///
           "cons_adj_food" "Food Consumption" ///
           "cons_adj_nfood" "Non-Food Consumption" ///
           "tot_adj_cons" "\shortstack[l]{\textit{Consumption} \\ Total Consumption per Capita}" ///
           "food_cons_adj" "Food Consumption" ///
           "nfood_cons_adj" "Non-Food Consumption" ///
           "tot_cons_adj" "\shortstack[l]{\textit{Panel A: Cons. \& Cons. Smoothing} \\ Total Consumption}" ///
           "ladder_now" "Life Satisfaction"  ///
           "job_satisfy_n" "Job Satisfaction"  ///
           "any_buy" "Buy Any Asset" ///
           "any_sell" "Sell Any Asset" ///
           "loan_borrow" "Taken Additional Loans" ///
           "loan_get" "Received gifts" ///
           "loan_give" "Gave gifts" ///
           "consmooth_move" "Moved" /// 
           "depressed" "\shortstack[l]{\textit{Panel A: Psychometric} \\ Feeling Depressed}" ///
           "depress_disint_n" "\shortstack[l]{\textit{Panel A: Psychometric} \\ Feeling Depressed/Disinterested}" ///
           "efficacy_n" "Self Efficacy" /// 
           "accomplish_goals" "Able to Accomplish Goals" /// 
           "covid_wage_loss" "Wage Loss During COVID" ///
           "looking_job" "Looking for Job" ///
           "job_search_online" "Used Online Platforms for Job Search" ///
           "job_search" "Searching for Job (Last Week)" ///
           "reg_job_mkt" "Register for Job Market" ///
           "contact_company" "Contact Company for Job" ///
           "advert_online" "Advertise Self for Job" ///
           "search_network" "Contact Network for Job" ///
           "cap_loc_lic" "Collecting Capital" ///
           "any_attempt" "Other Efforts in Job Search" ///
           "prep_new_bus" "Prepping New Business" ///
           "prep_bus_hh" "HH Prepping New Business" ///
           "hh_new_bus" "HH Prepping New Business" ///
           "business_ability" "Business Ability Confidence" ///
           "prog_subs" "Electricity and Water Subsidies During COVID" ///
           "prog_food_assist" "Grocery Assistance During COVID" ///
           "prog_direct_cash" "Direct Cash Assistance During COVID" ///
           "prog_other" "Other Assistance During COVID" ///
           "apply_kp" "Report Apply for Prakerja" ///
           "receive_kp" "Report Receive Prakerja" /// 
           "take_courses" "Took a course" ///
            "take_kp_courses" "Took a Prakerja course" ///
            "num_courses" "Number of courses taken" ///
            "course_hours" "Hours of Training" ///
            "course_completed" "Completed at least one course" ///   
            "course_cert_use" "Used certificate in job search" ///  
            "course_cert" "Received min. one course certificate" ///
            "course_pay_kp" "Paid for min. one course with Prakerja" ///  
            "course_online_live" "Took min. one course online live" ///
            "course_online" "Took min. one course online not live" ///     
            "course_offline" "Took min. one course in person" ///
            "ewallet_buy" "Made Any E-wallet Purchase" ///
            "views_gov_help_n" "\shortstack[l]{\textit{Panel C: Govt Program Beliefs} \\ Govt Responsible for Helping the Poor}" ///
            "views_allocation_n" "Assistance Should Prioritise the Poor" ///
            "win_before_surveyhh" "\shortstack[l]{\textit{Program Receipt} \\ HH Received Prakerja (PMO Data)}" ///
            "win_before_survey" "\shortstack[l]{\textit{Program Receipt} \\ Received Prakerja (PMO Data)}" ///
            "ever_win_17" "Win in Administrative Data"  ///
            "switch_industry_1" "Changed Industry from Feb '20" ///
            "wtp_online_training" "WTP for \$1 training credit" ///
            "ever_win_47" "Received Prakerja (PMO Data)"  ///
            "choose_ewallet" "Choose E-Wallet Over Phone-Credit" ///
			"net_ast_buy_w_mil" "Net Asset Purchases, Winsorized (million IDR)" ///
			"net_ast_buy_w_thous" "Net Asset Purchases" ///
            "respondent21" "\shortstack[l]{\textit{2021 Online Surevy} \\ Responded to and Completed Survey}" ///
            "respondent22" "\shortstack[l]{\textit{2022 Online Surevy} \\ Responded to and Completed Survey}")    
  end

* Export Table
* Inputs:
* columns: list of columns in resulting table
* table: table name

  cap program drop export_table_interact_sus
  program define export_table_interact_sus
    version 17.0
    args columns table order

    esttab `columns' using ///
      "`table'.tex", replace ///
      cells(b(star fmt(3)) se(par(( )) fmt(3)) dvmean(par([ ]) fmt(3)) pval(par(( )) fmt(3)))  ///
      stats(obs, label("Observations") fmt(%9.0fc)) noobs ///
      star(* 0.10 ** 0.05 *** 0.01) ///
      prehead("") posthead("") ///
      prefoot("\\ ") postfoot("") ///
      nonum not booktabs collabels(none) ///
      mlabels(none) ///
      order(`order') ///
      subs("prakerja_familiar" "Familiar with Prakerja" ///
           "report_applied" "Report Applying to Prakerja" ///
           "report_selectedhh" "HH Receive Winning Prakerja" ///
           "report_selected" "Report Receive Prakerja" ///
           "complete_pk_train" "Completed PK Training" ///
           "pk_improve_skill" "PK Improved Skill" ///
           "train_certif" "Received Training Certificate" ///
           "self_employed" "Self-Employed" ///
           "employed" "\shortstack[l]{\textit{Panel B: Employment} \\ Employed}" ///
           "self_emp_bus_owner" "Self Employed/Business Owner" ///
           "business_owner" "Business Owner" ///
           "perm_worker" "Permanent Worker" ///
           "temp_worker" "Temporary Worker" ///
           "family_unpaid" "Working for Family/Unpaid" ///
           "hours_worked" "Hours Worked" ///
           "jobhours_adj" "Hours Worked"  ///
           "multiple_jobs" "Working Multiple Jobs" ///
           "use_internet_work" "Use Internet at Work" ///
           "use_internet_bank" "Uses Internet for Banking" ///
           "use_internet" "\textit{Uses Internet}" ///
           "job_web" "\shortstack[l]{\textit{Digital Skills \& Comfort} \\ Used Internet on Job}"  ///
           "ind_adj_earnings" "Earnings" ///
           "ind_earnings_pos" "Earnings are Positive" ///
           "hh_adj_earnings" "HH Earnings" ///
           "hh_earnings_pos" "HH Earnings are Positive" ///
           "hourly_adj_wage" "Hourly Wage" ///
           "wage_adj" "Hourly Wage" ///
           "income_adj" "Total Earnings" ///
           "cons_adj_food" "Food Consumption" ///
           "cons_adj_nfood" "Non-Food Consumption" ///
           "tot_adj_cons" "\shortstack[l]{\textit{Panel A: Consumption} \\ Total Consumption per Capita}" ///
           "totcons" "\shortstack[l]{\textit{Panel A: Consumption} \\ Total Consumption}" ///
           "food_cons_adj" "Food Consumption" ///
           "nfood_cons_adj" "Non-Food Consumption" ///
           "tot_cons_adj" "\shortstack[l]{\textit{Cons. \& Cons. Smoothing} \\ Total Consumption}" ///
           "ladder_now" "Life Satisfaction"  ///
           "job_satisfy_n" "Job Satisfaction"  ///
           "any_buy" "Buy Any Asset" ///
           "any_sell" "Sell Any Asset" ///
           "loan_borrow" "Taken Additional Loans" ///
           "loan_get" "Received gifts" ///
           "loan_give" "Gave gifts" ///
           "consmooth_move" "Moved" /// 
           "depressed" "\shortstack[l]{\textit{Panel E: Psychometric} \\ Feeling Depressed}" ///
           "depress_disint_n" "\shortstack[l]{\textit{Panel E: Psychometric} \\ Feeling Depressed/Disinterested}" ///
           "efficacy_n" "Self Efficacy" /// 
           "accomplish_goals" "Able to Accomplish Goals" /// 
           "covid_wage_loss" "Wage Loss During COVID" ///
           "looking_job" "Looking for Job" ///
           "job_search_online" "Used Online Platforms for Job Search" ///
           "job_search" "Searching for Job (Last Week)" ///
           "reg_job_mkt" "Register for Job Market" ///
           "contact_company" "Contact Company for Job" ///
           "advert_online" "Advertise Self for Job" ///
           "search_network" "Contact Network for Job" ///
           "cap_loc_lic" "Collecting Capital" ///
           "any_attempt" "Other Efforts in Job Search" ///
           "prep_new_bus" "Prepping New Business" ///
           "prep_bus_hh" "HH Prepping New Business" ///
           "hh_new_bus" "HH Prepping New Business" ///
           "business_ability" "Business Ability Confidence" ///
           "prog_subs" "Electricity and Water Subsidies During COVID" ///
           "prog_food_assist" "Grocery Assistance During COVID" ///
           "prog_direct_cash" "Direct Cash Assistance During COVID" ///
           "prog_other" "Other Assistance During COVID" ///
           "apply_kp" "Report Apply for Prakerja" ///
           "receive_kp" "Report Receive Prakerja" /// 
           "take_courses" "\shortstack[l]{\textit{Training} \\ Took a course}" ///
            "take_kp_courses" "Took a Prakerja course" ///
            "num_courses" "Number of courses taken" ///
            "course_hours" "Hours of Training" ///
            "course_completed" "Completed at least one course" ///   
            "course_cert_use" "Used certificate in job search" ///  
            "course_cert" "Received min. one course certificate" ///
            "course_pay_kp" "Paid for min. one course with Prakerja" ///  
            "course_online_live" "Took min. one course online live" ///
            "course_online" "Took min. one course online not live" ///     
            "course_offline" "Took min. one course in person" ///
            "ewallet_buy" "Made Any E-wallet Purchase" ///
            "views_gov_help_n" "\shortstack[l]{\textit{Govt Program Beliefs} \\ Govt Responsible for Helping the Poor}" ///
            "views_allocation_n" "Assistance Should Prioritise the Poor" ///
            "win_before_surveyhh" "\shortstack[l]{\textit{Panel A: Program Receipt} \\ HH Received Prakerja (PMO Data)}" ///
            "win_before_survey" "\shortstack[l]{\textit{Panel A: Program Receipt} \\ Received Prakerja (PMO Data)}" ///
            "ever_win_17" "\shortstack[l]{\textit{Program Receipt} \\ Received Prakerja (PMO Data)}"  ///
            "switch_industry_1" "Changed Industry from Feb '20" ///
            "wtp_online_training" "WTP for \$1 training credit" ///
            "ever_win_47" "\shortstack[l]{\textit{Program Receipt} \\ Received Prakerja (PMO Data)}"  ///
            "choose_ewallet" "Choose E-Wallet Over Phone-Credit" ///
            "respondent21" "\shortstack[l]{\textit{2021 Online Surevy} \\ Responded to and Completed Survey}" ///
            "respondent22" "\shortstack[l]{\textit{2022 Online Surevy} \\ Responded to and Completed Survey}")    
  end

// END

  cap program drop export_table_interact
  program define export_table_interact
    version 17.0
    args columns table order

    esttab `columns' using ///
      "`table'.tex", replace ///
      cells(b(star fmt(3)) se(par(( )) fmt(3)) dvmean(par([ ]) fmt(3)) pval(par(( )) fmt(3)))  ///
      stats(obs, label("Observations") fmt(%9.0fc)) noobs ///
      star(* 0.10 ** 0.05 *** 0.01) ///
      prehead("") posthead("") ///
      prefoot("\\ ") postfoot("") ///
      nonum not booktabs collabels(none) ///
      mlabels(none) ///
      order(`order') ///
      subs("prakerja_familiar" "Familiar with Prakerja" ///
           "report_applied" "Report Applying to Prakerja" ///
           "report_selectedhh" "HH Receive Winning Prakerja" ///
           "report_selected" "Report Receive Prakerja" ///
           "complete_pk_train" "Completed PK Training" ///
           "pk_improve_skill" "PK Improved Skill" ///
           "train_certif" "Received Training Certificate" ///
           "self_employed" "Self-Employed" ///
           "employed" "Employed" ///
           "self_emp_bus_owner" "Self Employed/Business Owner" ///
           "business_owner" "Business Owner" ///
           "perm_worker" "Permanent Worker" ///
           "temp_worker" "Temporary Worker" ///
           "family_unpaid" "Working for Family/Unpaid" ///
           "hours_worked" "Hours Worked" ///
           "jobhours_adj" "Hours Worked"  ///
           "multiple_jobs" "Working Multiple Jobs" ///
           "use_internet_work" "Use Internet at Work" ///
           "use_internet_bank" "Uses Internet for Banking" ///
           "use_internet" "\textit{Uses Internet}" ///
           "job_web" "\shortstack[l]{\textit{Digital Skills \& Comfort} \\ Used Internet on Job}"  ///
           "ind_adj_earnings" "Earnings" ///
           "ind_earnings_pos" "Earnings are Positive" ///
           "hh_adj_earnings" "HH Earnings" ///
           "hh_earnings_pos" "HH Earnings are Positive" ///
           "hourly_adj_wage" "Hourly Wage" ///
           "wage_adj" "Hourly Wage" ///
           "income_adj" "Total Earnings" ///
           "cons_adj_food" "Food Consumption" ///
           "cons_adj_nfood" "Non-Food Consumption" ///
           "tot_adj_cons" "\shortstack[l]{\textit{Panel A: Consumption} \\ Total Consumption per Capita}" ///
           "food_cons_adj" "Food Consumption" ///
           "nfood_cons_adj" "Non-Food Consumption" ///
           "tot_cons_adj" "\shortstack[l]{\textit{Cons. \& Cons. Smoothing} \\ Total Consumption}" ///
           "ladder_now" "Life Satisfaction"  ///
           "job_satisfy_n" "Job Satisfaction"  ///
           "any_buy" "Buy Any Asset" ///
           "any_sell" "Sell Any Asset" ///
           "loan_borrow" "Taken Additional Loans" ///
           "loan_get" "Received gifts" ///
           "loan_give" "Gave gifts" ///
           "consmooth_move" "Moved" /// 
           "depressed" "\shortstack[l]{\textit{Panel E: Psychometric} \\ Feeling Depressed}" ///
           "depress_disint_n" "\shortstack[l]{\textit{Panel E: Psychometric} \\ Feeling Depressed/Disinterested}" ///
           "efficacy_n" "Self Efficacy" /// 
           "accomplish_goals" "Able to Accomplish Goals" /// 
           "covid_wage_loss" "Wage Loss During COVID" ///
           "looking_job" "Looking for Job" ///
           "job_search_online" "Used Online Platforms for Job Search" ///
           "job_search" "Searching for Job (Last Week)" ///
           "reg_job_mkt" "Register for Job Market" ///
           "contact_company" "Contact Company for Job" ///
           "advert_online" "Advertise Self for Job" ///
           "search_network" "Contact Network for Job" ///
           "cap_loc_lic" "Collecting Capital" ///
           "any_attempt" "Other Efforts in Job Search" ///
           "prep_new_bus" "Prepping New Business" ///
           "prep_bus_hh" "HH Prepping New Business" ///
           "hh_new_bus" "HH Prepping New Business" ///
           "business_ability" "Business Ability Confidence" ///
           "prog_subs" "Electricity and Water Subsidies During COVID" ///
           "prog_food_assist" "Grocery Assistance During COVID" ///
           "prog_direct_cash" "Direct Cash Assistance During COVID" ///
           "prog_other" "Other Assistance During COVID" ///
           "apply_kp" "Report Apply for Prakerja" ///
           "receive_kp" "Report Receive Prakerja" /// 
           "take_courses" "\shortstack[l]{\textit{Training} \\ Took a course}" ///
            "take_kp_courses" "Took a Prakerja course" ///
            "num_courses" "Number of courses taken" ///
            "course_hours" "Hours of Training" ///
            "course_completed" "Completed at least one course" ///   
            "course_cert_use" "Used certificate in job search" ///  
            "course_cert" "Received min. one course certificate" ///
            "course_pay_kp" "Paid for min. one course with Prakerja" ///  
            "course_online_live" "Took min. one course online live" ///
            "course_online" "Took min. one course online not live" ///     
            "course_offline" "Took min. one course in person" ///
            "ewallet_buy" "Made Any E-wallet Purchase" ///
            "views_gov_help_n" "\shortstack[l]{\textit{Govt Program Beliefs} \\ Govt Responsible for Helping the Poor}" ///
            "views_allocation_n" "Assistance Should Prioritise the Poor" ///
            "win_before_surveyhh" "\shortstack[l]{\textit{Panel A: Program Receipt} \\ HH Received Prakerja (PMO Data)}" ///
            "win_before_survey" "\shortstack[l]{\textit{Panel A: Program Receipt} \\ Received Prakerja (PMO Data)}" ///
            "ever_win_17" "\shortstack[l]{\textit{Program Receipt} \\ Received Prakerja (PMO Data)}"  ///
            "switch_industry_1" "Changed Industry from Feb '20" ///
            "wtp_online_training" "WTP for \$1 training credit" ///
            "ever_win_47" "\shortstack[l]{\textit{Program Receipt} \\ Received Prakerja (PMO Data)}"  ///
            "choose_ewallet" "Choose E-Wallet Over Phone-Credit" ///
			"net_ast_buy_w_mil" "Net Asset Purchases, Winsorized (million IDR)" ///
            "respondent21" "\shortstack[l]{\textit{2021 Online Surevy} \\ Responded to and Completed Survey}" ///
            "respondent22" "\shortstack[l]{\textit{2022 Online Surevy} \\ Responded to and Completed Survey}")    
  end